<?php
require_once("./define/define_table.php");
require_once("./class/Association.php");
require_once("./class/Partenaire.php");
define("ADD_FINANCEMENT_FORM", "Formulaire d'ajout d'une source de financement");
define("MODIFY_FINANCEMENT_FORM", "Formulaire de modification d'une source de financement");

class Financement {
	
	private $id_financement;
	private $id_partenaire;
	private $id_association;
	private $montant;
	private $date_financement;
	
	public function Financement() {

	}
	
	public function getDateFinancement(){
	    return $this->date_financement;
	}
	
	public function getIdFinancement(){
		return $this->id_financement;
	}
	
	public function getIdAsso(){
		return $this->id_association;
	}
	
	public function getIdPartenaire(){
		return $this->id_partenaire;
	}
	
	public function getMontant(){
		return $this->montant;
	}
	
	public function setIdFinancement($id){
		$this->id_financement = $id;
	}
	
	public function setIdAsso($id){
		$this->id_association = $id;
	}
	
	public function setIdPartenaire($id){
		$this->id_partenaire = $id;
	}
	
	public function setMontant($montant){
		$this->montant = $montant;
	}
	
	public function setDateFinancement($date){
	    $this->date_financement = $date;
	}
	
	public function getAllFinancement(){
	    global $db;
		try{
			$sql = "SELECT id_financement,montant, id_partenaire, id_association, date_financement FROM ".FINANCEMENT_TABLE;
			$sth = $db->prepare($sql);
			$sth->execute();
			return $sth->fetchAll(PDO::FETCH_CLASS, "Financement");
		}catch(PDOException $e){
			echo $e.getMessage();
		}
	}
	
	public function getFinancementByAsso($id_association){
		global $db;
		try{
			$sql = "SELECT montant, id_partenaire, id_association, date_financement FROM ".FINANCEMENT_TABLE." WHERE id_association = :id_association";
			$sth = $db->prepare($sql);
			$sth->bindValue(":id_association", $id_association, PDO::PARAM_INT);
			$sth->execute();
			PrintQuery::queryPrint($sth);
			return $sth->fetchAll(PDO::FETCH_CLASS, "Financement");
		}catch(PDOException $e){
			echo $e.getMessage();
		}
	}
	
	public function getFinancementBySource($id_partenaire){
		global $db;
		try{
			$sql = "SELECT montant, id_partenaire, id_association, date_financement FROM ".FINANCEMENT_TABLE." WHERE id_partenaire= :id_partenaire";
			$sth = $db->prepare($sql);
			$sth->bindValue(":id_partenaire", $id_partenaire, PDO::PARAM_INT);
			$sth->execute();
			PrintQuery::queryPrint($sth);
			return $sth->fetchAll(PDO::FETCH_CLASS, "Financement");
		}catch(PDOException $e){
			echo $e.getMessage();
		}
	}

	public function getFinancementAll($id_partenaire, $id_association){
		global $db;
		try{
			$sql = "SELECT montant, id_partenaire, id_association, date_financement FROM ".FINANCEMENT_TABLE." WHERE id_partenaire= :id_partenaire AND id_association = :id_association";
			$sth = $db->prepare($sql);
			$sth->bindValue(":id_partenaire", $id_partenaire, PDO::PARAM_INT);
			$sth->bindValue(":id_association", $id_association, PDO::PARAM_INT);
			$sth->execute();
			PrintQuery::queryPrint($sth);
			return $sth->fetchAll(PDO::FETCH_CLASS, "Financement");
		}catch(PDOException $e){
			echo $e.getMessage();
		}
	}

	public function getFinancement($id_financement){
		global $db;
		try{
			$sql = "SELECT id_financement, id_partenaire, id_association, montant, date_financement FROM ".FINANCEMENT_TABLE." WHERE id_financement= :id_financement";
			$sth = $db->prepare($sql);
			$sth->bindValue(":id_financement", $id_financement, PDO::PARAM_INT);
			$sth->execute();
			return $sth->fetchObject("Financement");
		}catch(PDOException $e){
			echo $e.getMessage();
		}
	}

	public function addOrModify(){
		global $db;
		$res = self::getFinancement($this->id_financement);
		try{
			if($res == false) {
				$sql = "INSERT INTO ".FINANCEMENT_TABLE." VALUES(null, :id_partenaire, :id_association, :montant, :date_financement)";
				$sth = $db->prepare($sql);
				$sth->bindValue(":id_association", $this->id_association, PDO::PARAM_INT);
				$sth->bindValue(":id_partenaire", $this->id_partenaire, PDO::PARAM_INT);
				$sth->bindValue(":date_financement", $this->date_financement);
				$sth->bindValue(":montant", $this->montant);
			}
			else{
				$sql = "UPDATE ".FINANCEMENT_TABLE." SET montant = :montant, date_financement = :date_financement WHERE id_association= :id_association AND id_partenaire = :id_partenaire";
				$sth = $db->prepare($sql);
				$sth->bindValue(":montant", $this->montant);
				$sth->bindValue(":date_financement", $this->date_financement);
				$sth->bindValue(":id_association", $this->id_association, PDO::PARAM_INT);
				$sth->bindValue(":id_partenaire", $this->id_partenaire, PDO::PARAM_INT);
			}
			PrintQuery::queryPrint($sth);
			return $sth->execute(); 
		}catch(PDOException $e){
			echo $e.getMessage();
		}
	}
	
	public static function getAddForm(){
		$html = ADD_FINANCEMENT_FORM .
				'<form class="ajax_form" id="add_financement_form" action="?module=financement&action=doAdd" method="post">' .
				'	<table>' .
				'		<tr>' .
				'			<td>'._ID_ASSOCIATION_.'</td>' .
				'			<td>'.Association::generateSelect().'</td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._ID_PARTENAIRE_.'</td>' .
				'			<td>'.Partenaire::generateSelect().'</td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._MONTANT_FINANCEMENT_.'</td>' .
				'			<td><input type="text" name="montant" /></td>' .
				'		</tr>' .
		        '		<tr>' .
				'			<td>'._DATE_FINANCEMENT_.'</td>' .
				'			<td><input type="text" name="date_financement" class="datepicker" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td colspan="2"><input type="submit" />' .
				'		</tr>' .
				'	</table>' .
				'</form>';
		return $html;
	}
	
	public function getModifyForm(){
		$html = MODIFY_FINANCEMENT_FORM .
				'<form class="ajax_form" id="modify_financement_form" action="?module=financement&action=doModify" method="post">' .
				'	<table>' .
				'		<tr>' .
				'			<td>'._ID_FINANCEMENT_.'</td>' .
				'			<td><input readonly type="text" name="id_financement" value = "'.$this->id_financement.'"/></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._ID_ASSOCIATION_.'</td>' .
				'			<td><input readonly type="text" name="id_association" value = "'.$this->id_association.'"/></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._ID_PARTENAIRE_.'</td>' .
				'			<td><input readonly type="text" name="id_partenaire" value = "'.$this->id_partenaire.'" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._MONTANT_FINANCEMENT_.'</td>' .
				'			<td><input type="text" name="montant" value = "'.$this->montant.'" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._DATE_FINANCEMENT_.'</td>' .
				'			<td><input type="text" name="date_financement" value = "'.$this->date_financement.'" class="datepicker" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td colspan="2"><input type="submit" />' .
				'		</tr>' .
				'	</table>' .
				'</form>';
		return $html;
	}

	public function delete(){
	    global $db;
	    $sql = "DELETE FROM ".FINANCEMENT_TABLE."
							WHERE id_financement = :id_financement";
	    $sth = $db->prepare($sql);
	    $sth->bindValue(":id_financement", $this->id_financement, PDO::PARAM_INT);
	    $sth->execute();
	    PrintQuery::queryPrint($sth);
	}
}
?>
